package game.games.carcassonne;

import game.GameConfig;
import game.coord.IterablePoint;
import game.gfx.GraphicUtil;
import game.gfx.cellmap.AbstractCellMapGame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;

import javax.swing.SwingUtilities;

public class Carcassonne extends AbstractCellMapGame<CarcassonneMap>
{
    private boolean            OVERRIDE_ENABLED = true;
    
    private int currentTileIndex = 0;
    private int currentTileAngle = 0;
    private int border           = 1;
    
    public Carcassonne()
    {
        super(true, new CarcassonneMap());
        getGameListener().addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if (isRunning())
                {
                    if (SwingUtilities.isRightMouseButton(e))
                    {
                        if (e.isControlDown())
                        {
                            currentTileIndex = 0;
                            currentTileAngle = 0;
                        }
                        else
                        {
                            currentTileAngle += 90;
                            if (currentTileAngle >= 360)
                            {
                                currentTileAngle = 0;
                            }
                        }
                    }
                    else if (SwingUtilities.isLeftMouseButton(e))
                    {
                        Point coords = getMousePoint();
                        if (isOverrideEnabled() || getMap().getTile(coords.x, coords.y) == null)
                        {
                            AngledTile tile = TileLoader.getTile(currentTileIndex);
                            tile.setAngle(currentTileAngle);
                            getMap().getCells().setValue(coords, tile);
                        }
                    }
                }
            }
        });
        
        getGameListener().addMouseWheelListener(new MouseWheelListener() 
        {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e)
            {
                int rotation = e.getWheelRotation();
                int index = currentTileIndex + rotation;
                if (index < 0)
                {
                    index += TileLoader.sumTiles();
                }
                if (index >= TileLoader.sumTiles())
                {
                    index -= TileLoader.sumTiles();
                }
                currentTileIndex = index;
                currentTileAngle = 0;
            }
        });
    }
    
    @Override
    protected void initConfig()
    {
        getConfig().setFalse(GameConfig.CONSOLE_ENABLED);
    }
    
    public boolean isOverrideEnabled()
    {
        return OVERRIDE_ENABLED;
    }
    
    @Override
    protected void doNext()
    {
    }
    
    @Override
    protected void onStart()
    {
        // size
        Dimension size = new Dimension(15, 15);
        getMap().setMapSize(size);
        Dimension mapSize = getMap().getCellMapSize();
        setPanelSize(mapSize);
    }
    
    @Override
    public BufferedImage getImage()
    {
        Dimension mapSize = getMap().getCellMapSize();
        // image
        BufferedImage image = new BufferedImage(mapSize.width, mapSize.height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D imageGraphics = image.createGraphics();
        // Weißer Hintergrund
        GraphicUtil.fillRect(imageGraphics, new Point(border, border), new Dimension(mapSize.width - (border * 2), mapSize.height - (border * 2)), Color.WHITE);
        for (IterablePoint<AngledTile> i = getMap().getCells().getIterator(); i.hasNext();)
        {
            Point position = i.getCurrentPoint();
            AngledTile tile = i.next();
            BufferedImage img = null;
            if (tile.getTile() != null && (isRunning() && position.equals(getMousePoint()) && isOverrideEnabled()) == false)
            {
                img = tile.getTile().getBitmap();
                int angle = tile.getAngle();
                if (img != null && angle != 0)
                {
                    img = GraphicUtil.rotateImage(img, angle);
                }
            }
            else
            {
                if (isRunning() && position.equals(getMousePoint()))
                {
                    img = TileLoader.getTile(currentTileIndex).getTile().getBitmap();
                    if (currentTileAngle != 0)
                    {
                        img = GraphicUtil.rotateImage(img, currentTileAngle);
                    }
                }
            }
            if (img != null)
            {
                GraphicUtil.drawImage(imageGraphics, position, img, border);
            }
            getMap().drawRect(imageGraphics, position, Color.BLACK);
        }
        return image;
    }
    
    public static void main(String[] args)
    {
        Thread t = new Thread(new Carcassonne(), "Carcassonne");
        t.start();
    }
}
